{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b291910e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['A', 'B', 'C', 'D', 'E']\n",
      "A\n",
      "E\n",
      "dog\n",
      "110\n",
      "D\n",
      "1\n",
      "['cat', 120, 'C']\n",
      "[['a', 'b', 'c'], ['1', '2', '3']]\n",
      "['1', '2', '3']\n",
      "b\n",
      "C\n",
      "B\n",
      "B\n",
      "['A', 'B', 'C', 'D']\n",
      "['B', 'C']\n",
      "['B', 'C']\n",
      "['C', 'D']\n",
      "['A', 'B', 'C', 'D']\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'list' object is not callable",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-89b13c50c508>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     70\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     71\u001b[0m \u001b[1;31m# 5. 特殊 可以指定 slice切片的步长 step\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 72\u001b[1;33m \u001b[0mnum_list\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     73\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     74\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# print(num_list[起始值：结束值：步长]  默认值为[0：表中的最大值：1]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'list' object is not callable"
     ]
    }
   ],
   "source": [
    "# 列表list\n",
    "# 构建列表 形式[] 其中元素以\" , \"隔开    列表也是一个变量\n",
    "# 列表是有顺序的一组值，顺序是从0位置开始\n",
    "# 相关学习文档在：https://docs.python.org/3/tutorial/introduction.html#lists\n",
    "\n",
    "names_str = [\"A\",\"B\",\"C\",\"D\",\"E\"]\n",
    "print(names_str)\n",
    "\n",
    "# 应用1：取值（从列表中取出字母）  从0开始取 各个字母所对应的位置数：A-0 B-1 C-2 D-3 E-4\n",
    "print(names_str[0])  # 取出列表中的第一个值\n",
    "print(names_str[4])  # 取出列表最后一个\n",
    "\n",
    "# 应用2：一次取多个对应值\n",
    "user_name_list = [\"dog\",\"cat\",\"rabbit\"]\n",
    "password_list = [110,120,119]\n",
    "user_data_list = [\"D\",\"C\",\"R\"]\n",
    "usrr_age_list = [1,2,3]\n",
    "# ......  还可以添加多个list\n",
    "\n",
    "print(user_name_list[0])  # 一次取出每个列表中的第一个值\n",
    "print(password_list[0])\n",
    "print(user_data_list[0])\n",
    "print(usrr_age_list[0])\n",
    "# ......  同理也可以打印出多个\n",
    "\n",
    "# 列表嵌套一\n",
    "info_list = [[\"dog\",110,\"D\"],[\"cat\",120,\"C\"],[\"rabbit\",119,\"R\"]]\n",
    "print(info_list[1])   #  所得的值为['cat', 120, 'C']\n",
    "# 列表嵌套二\n",
    "a = [\"a\",\"b\",\"c\"]\n",
    "b = [\"1\",\"2\",\"3\"]\n",
    "x = [a,b]\n",
    "print(x)\n",
    "print(x[1])  # 得 [1,2,3]\n",
    "print(x[0][1])  # 结果为b ,因为先在x中取出列表a[a,b,c]，再从列表a中取出位置数为1的值 b\n",
    "\n",
    "# 数据结构之列表的切片\n",
    "# 切片的查询\n",
    "    # -4,-3,-2,-1,0,1,2,3 (只有正数才有0，而负数没有可以理解为在python中每个变量只能用一次，不能重复)\n",
    "    # slices(切片)数值是指值（values）在list列表中的位置\n",
    "names = [\"A\",\"B\",\"C\",\"D\"]\n",
    "# 取值有两种方法 单索引（单个值）  和  slice切片（多个或者单个值）\n",
    "# 1. 正数即正向取值 从左到右\n",
    "# 每个字符串所对应的位置数：A-0；B-1；C-2；D-3\n",
    "print(names[2])  # 所取出的值为C\n",
    "# 2. 负数即方向取值 从右到左\n",
    "# 每个字符串所对应的位置数：A—— -4 ; B—— -3 ; C—— -2 ; D—— -1\n",
    "print(names[-3])   # 所取出的值为B\n",
    "# 3.特殊\n",
    "# print(names[4]) # IndexError: list index out of range  所取的值不在列表范围内所以取不到 所以会报错\n",
    "\n",
    "# 补充列表的单索引用法：与切片的规则一致\n",
    "# 1.列表中的所有元素都有编号——从0开始递增\n",
    "# 2.索引只一次只能取出一个值相对于列表来说\n",
    "# 3.注意区分单索引索引和index() 函数的概念\n",
    "list = [\"A\",\"B\",\"C\"]\n",
    "print(list[1])\n",
    "\n",
    "# 切片的另一种写法\n",
    "names = [\"A\",\"B\",\"C\",\"D\"]\n",
    "# 1. 取所有的值\n",
    "print(names[:]) # 若要从0开始取值 可以不用写0，python默认0开始取值\n",
    "# 2. 正数（正向取值） 从左往右取值时，右边的值取不到\n",
    "print(names[1:3])\n",
    "# 3. 负数（方向取值） 从右往左取值时，右边的值取不到 且从取的数值位置大小来看 ，更大的值要放在右侧（-4：-2）\n",
    "print(names[-3:-1])\n",
    "# 4.若右侧不写任何值则可以取到右侧的值 最后一个值一定不能写0\n",
    "print(names[2:])\n",
    "print(names[-4:])\n",
    "\n",
    "# 5. 特殊 可以指定 slice切片的步长 step\n",
    "num_list = list(range(10))\n",
    "print(num_list)\n",
    "print(num_list[2:9:3]) # print(num_list[起始值：结束值：步长]  默认值为[0：表中的最大值：1]\n",
    "# 所得结果为[0,1,2,3,4,5,6,7,8,9]\n",
    "# 所得的值为 2，5，8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae1f7d16",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
